package com.shopmall.goods.service.impl;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.shopmall.goods.dao.GoodsAttrValueMapper;
import com.shopmall.goods.dao.GoodsInfoMapper;
import com.shopmall.goods.entity.GoodsAttrValue;
import com.shopmall.goods.entity.GoodsInfo;
import com.shopmall.goods.service.GoodsInfoService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements GoodsInfoService {
    @Resource
    private GoodsAttrValueMapper goodsAttrValueMapper;
    @Override
    public IPage<GoodsInfo> pageList(GoodsInfo goodsInfo) {
        IPage<GoodsInfo> page = new Page<>(goodsInfo.getPageNum(),goodsInfo.getPageSize());
        LambdaQueryWrapper<GoodsInfo> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(goodsInfo.getGoodsName())){
            wrapper.eq(GoodsInfo::getGoodsName,goodsInfo.getGoodsName());
        }
        IPage<GoodsInfo> p = this.baseMapper.selectPage(page, wrapper);
        return p;

    }
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    ElasticsearchClient elasticsearchClient;
    @Override
    public int addOrUpdate(GoodsInfo goodsInfo) {
        if(goodsInfo.getId()==null){
            int insert = this.baseMapper.insert(goodsInfo);
            Integer id = goodsInfo.getId();
            List<GoodsAttrValue> goodsAttrValueList = goodsInfo.getGoodsAttrValueList();
            if(goodsAttrValueList!=null){
                for (GoodsAttrValue goodsAttrValue : goodsAttrValueList) {
                    goodsAttrValue.setGoodsId(id);
                    goodsAttrValueMapper.insert(goodsAttrValue);
                }
            }
            return insert;
        }else{
            goodsAttrValueMapper.delete(new LambdaQueryWrapper<GoodsAttrValue>().eq(GoodsAttrValue::getGoodsId,goodsInfo.getId()));
            List<GoodsAttrValue> goodsAttrValueList = goodsInfo.getGoodsAttrValueList();
            if(goodsAttrValueList!=null){
                for (GoodsAttrValue goodsAttrValue : goodsAttrValueList) {
                    goodsAttrValue.setGoodsId(goodsInfo.getId());
                    goodsAttrValueMapper.insert(goodsAttrValue);
                }
            }
            int i = this.baseMapper.updateById(goodsInfo);
            if(i>0){
                String s = JSON.toJSONString(goodsInfo);
                //发送消息
//                rabbitTemplate.convertAndSend("76_exchange","routingKey",s);
            }
            return this.baseMapper.updateById(goodsInfo);
        }

    }

    @Override
    public int deleteById(Integer id) {
        goodsAttrValueMapper.delete(new LambdaQueryWrapper<GoodsAttrValue>().eq(GoodsAttrValue::getGoodsId,id));
        return baseMapper.deleteById(id);
    }

}
